package com.ykw.parking.Utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class UploadImage {
    public static final String base_path="C:\\Users\\凯文的computer\\IdeaProjects\\parking\\src\\main\\resources\\static\\saveimage\\";
    public static final String access_path="http://localhost:8080/uploadView/";
    public static String upload(MultipartFile file){
        //获得上传文件的名称
        String filename = file.getOriginalFilename();
        //为了保证图片在服务器中的唯一性,我们用uuid对filename进行改写
        String uuid= UUID.randomUUID().toString().replace("-","");
        String newFilename=uuid+"-"+filename;
        //创建一个文件实例
        File image = new File(base_path, newFilename);
        //对这个图片进行上传
        try {
            file.transferTo(image);
        } catch (IOException e) {
           return null;
        }
        return access_path+newFilename;
    }
}
